package com.lashou.labelutil.dao;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lashou.labelutil.model.GoodsCategoryLabelLog;
import com.lashou.labelutil.model.GoodsCategoryList;
import com.lashou.labelutil.model.GoodsProfileBean;
import com.lashou.labeutil.util.TimeUtil;

public class LabelDAOImpl extends BaseDAO implements ILabelDAO {

	private static final long serialVersionUID = 8432075795167981401L;

	@SuppressWarnings("unchecked")
	public List<GoodsProfileBean> findAllGoodsProfileBean()
	{
		String sql = "select * from goods_profile order by goods_id desc";
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql);

		List<GoodsProfileBean> beans = new ArrayList<GoodsProfileBean>();

		if (beansMapList == null || beansMapList.size() < 1)
		{
			return beans;
		}

		for (Map<String, Object> dealMap : beansMapList)
		{
			GoodsProfileBean bean = populateBeanFromMap(dealMap);
			beans.add(bean);
		}	
		return beans;
	}
	
	private GoodsProfileBean populateBeanFromMap(Map<String, Object> beanMap)
	{
		GoodsProfileBean bean = new GoodsProfileBean();
		bean.setGoodsId((Integer)beanMap.get("goods_id"));
		bean.setTitle((String)beanMap.get("title"));
		
		if(null!=beanMap.get("start_time"))
		{
			Date st= TimeUtil.getDateByLong((Long)beanMap.get("start_time"));
			bean.setStartTime(TimeUtil.getYMDHMSTime(st));
		}
		
		if(null!=beanMap.get("end_time"))
		{
			Date et = TimeUtil.getDateByLong((Long)beanMap.get("end_time"));
			bean.setEndTime(TimeUtil.getYMDHMSTime(et));
		}
		

		bean.setCategory((String)beanMap.get("category"));
		return bean;
	}
	
	
	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsCategoryList> findAllGoodsCategoryList() {
		String sql = "select * from goods_category_list";
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql);
		
		List<GoodsCategoryList> beans = new ArrayList<GoodsCategoryList>();
		if (beansMapList == null || beansMapList.size() < 1)
		{
			return beans;
		}
		for (Map<String, Object> dealMap : beansMapList)
		{
			GoodsCategoryList bean = populateGoodsCategoryFromMap(dealMap);
			beans.add(bean);
		}	
		
		return beans;
	}
	private GoodsCategoryList populateGoodsCategoryFromMap(Map<String, Object> beanMap)
	{
		GoodsCategoryList bean = new GoodsCategoryList();

		bean.setGoodsCategoryId((Integer)beanMap.get("goods_category_id"));
		bean.setGoodsCategoryName((String)beanMap.get("goods_category_name"));  
		bean.setGoodsCategoryNameZh((String)beanMap.get("goods_category_name_zh"));
		bean.setValid(Integer.valueOf(beanMap.get("valid")+""));
		return bean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Map<String, GoodsCategoryLabelLog> findAllGoodsCategoryLabelLog() {
		String sql = "select * from goods_category_label_log";
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql);
		
		Map<String,GoodsCategoryLabelLog> beans = new HashMap<String,GoodsCategoryLabelLog>();
		if (beansMapList == null || beansMapList.size() < 1)
		{
			return beans;
		}
		for (Map<String, Object> dealMap : beansMapList)
		{
			GoodsCategoryLabelLog bean = populateGoodsCategoryLabelFromMap(dealMap);
			beans.put(bean.getGoodsId()+"", bean);
		}	
		
		return beans;
	}
	
	private GoodsCategoryLabelLog populateGoodsCategoryLabelFromMap(Map<String, Object> beanMap)
	{
		GoodsCategoryLabelLog bean = new GoodsCategoryLabelLog();

		bean.setGoodsId((Integer)beanMap.get("goods_id"));
		bean.setCategoryLogs((String)beanMap.get("category_logs"));  
	
		return bean;
	}

	@Override
	public int getGoodsProfileTotal() {
		try
		{
			String sql = "select count(goods_id) from goods_profile ";	
			
			return this.getJdbcTemplate().queryForInt(sql);
		}catch(Exception e)
		{
			
		}
		return 0;
	}

	@Override
	public int getGoodsLabelLogTotal() {
		try
		{
			String sql = "select count(goods_id) from goods_category_label_log";
			return this.getJdbcTemplate().queryForInt(sql);
		}catch(Exception e)
		{
			
		}
		return 0;
	}

	@Override
	public boolean updateGoodsProfileBean(GoodsProfileBean bean) {
		String sql = "update goods_profile set category = ? where goods_id = ?";
		
		try
		{
			this.getJdbcTemplate().update(
					sql,new Object[]{bean.getCategory(),bean.getGoodsId()});
		}catch(Exception e)
		{
			return false;
		}
		return true;
	}

	@Override
	public boolean saveGoodsCategoryLabelLog(GoodsCategoryLabelLog log) {
		String sql = "insert into goods_category_label_log(goods_id,category_logs) values(?,?)";
		try
		{
			this.getJdbcTemplate().update(
					sql,new Object[]{log.getGoodsId(),log.getCategoryLogs()});
		}catch(Exception e)
		{
			return false;
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsProfileBean> findGoodsProfileByCategoryType(
			String categoryType, int startIndex, int count) {
		String sql = "select * from goods_profile "+("-1".equals(categoryType)?"":"where category like '%"+categoryType+"%'")+" group by title order by goods_id limit ?,?";
		if("-2".equals(categoryType))
		{
			sql = "select * from goods_profile where category is null or category  = '' or category  = '0;' or category  = '1;' or category  = '2;' or category  = '3;' or category  = '4;' order by goods_id limit ?,? ";
		}
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql,new Object[]{startIndex,count});

		List<GoodsProfileBean> beans = new ArrayList<GoodsProfileBean>();

		if (beansMapList == null || beansMapList.size() < 1)
		{
			return beans;
		}

		for (Map<String, Object> dealMap : beansMapList)
		{
			GoodsProfileBean bean = populateBeanFromMap(dealMap);
			beans.add(bean);
		}	
		return beans;
	}

	@Override
	public int findBySQL(String sql) {
		try
		{
			return this.getJdbcTemplate().queryForInt(sql);
		}
		catch(Exception e)
		{
			
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	@Override
	public GoodsProfileBean findGoodsProfileBeanById(Serializable goodsId) {
		String sql = "select * from goods_profile where goods_id = ?";
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql,new Object[]{goodsId});

		GoodsProfileBean bean = new GoodsProfileBean();

		if (beansMapList == null || beansMapList.size() < 1)
		{
			return bean;
		}

		for (Map<String, Object> dealMap : beansMapList)
		{
			bean = populateBeanFromMap(dealMap);
			
		}	
		return bean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsProfileBean> findGoodsProfileBeanByTitle(Serializable title) {
		
		String sql = "select * from goods_profile where title = ?";
		List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql,new Object[]{title});

		List<GoodsProfileBean> beans = new ArrayList<GoodsProfileBean>();

		if (beansMapList == null || beansMapList.size() < 1)
		{
			return beans;
		}

		for (Map<String, Object> dealMap : beansMapList)
		{
			GoodsProfileBean bean = populateBeanFromMap(dealMap);
			beans.add(bean);
		}	
		return beans;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<GoodsProfileBean> findLastGoodsProfile(long date,
			int startIndex, int count) {
			String sql = "select * from goods_profile where start_time >= ? group by title order by start_time limit ?,?";
			List<Map<String, Object>> beansMapList = this.getJdbcTemplate().queryForList(sql,new Object[]{date,startIndex,count});
	
			List<GoodsProfileBean> beans = new ArrayList<GoodsProfileBean>();
	
			if (beansMapList == null || beansMapList.size() < 1)
			{
				return beans;
			}
	
			for (Map<String, Object> dealMap : beansMapList)
			{
				GoodsProfileBean bean = populateBeanFromMap(dealMap);
				beans.add(bean);
			}	
			return beans;
	}

	@Override
	public int findTotalLastGoodsProfile(long date) {
		String sql = "select count(c.cu) from ( select count(goods_id) cu from goods_profile where start_time >= ? group by title ) c";
		try{
			return this.getJdbcTemplate().queryForInt(sql, new Object[]{date});
		}catch(Exception e)
		{
			return 0;
		}
	}

}
